package com.hycreate.platform.modules.crm.dataImport;



import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.utils.excel.ImportExcel;
import com.hycreate.platform.modules.crm.business.Customer;
import com.hycreate.platform.modules.crm.business.CustomerService;



@Controller
@RequestMapping(value = "${adminPath}/crm/dataImport/baseInformation")
public class DataImportController {
	
	@Autowired
	private CustomerService customerService;
	List<Customer> listNewInsert = null;
	List<Customer> listAlreadyExist = null;
	/**
     * 导入电子表格中的数据
     * @param 
     * @return
	 * @throws IOException 
	 * @throws InvalidFormatException 
     */
	@RequestMapping(value = "getAllByExcel")
    public void  getAllByExcel(HttpServletRequest request,HttpServletResponse response)
    		throws InvalidFormatException, IOException{
		request.setCharacterEncoding("utf-8");
		listNewInsert = new ArrayList<Customer>();
		listAlreadyExist = new ArrayList<Customer>();


		String path = request.getSession().getServletContext().getRealPath("pom.xml");
		path = path.substring(0,path.length()-21);
		String file = request.getParameter("file");
		file = URLDecoder.decode(file,"UTF-8");
		file = path + file;
        String province = request.getParameter("province");
        String city = request.getParameter("city");
        String address = request.getParameter("address");
        address = URLDecoder.decode(address,"UTF-8");
        String trade = request.getParameter("trade");
        trade = URLDecoder.decode(trade,"UTF-8");
  
        ImportExcel im  = new ImportExcel(file,1);

       for (int i = 1; i < im.getLastDataRowNum(); i++) {//行
            //第一个是列数，第二个是行数
    	   	Customer customer = new Customer();
        	String name = (String) im.getCellValue(im.getRow(i),0);

        	String content = (String) im.getCellValue(im.getRow(i), 6);

            String website = (String) im.getCellValue(im.getRow(i), 7);
  
            customer.setName(name);
            customer.setAddress(address);
            customer.setTrade(trade);
            customer.setContent(content);
            customer.setWebsite(website);
            customer.setProvince(province);
            customer.setCity(city);
            customer.setStatusId("暂无意向");
            if(customerService.findMyList(name)==null){
            	customerService.save(customer);
            }else{
            	listNewInsert.add(customer);
            	listAlreadyExist.add(customerService.findMyList(name));//重复，数据库中已存在的公司
            }
            
        }
      
      ObjectMapper mapper = new ObjectMapper();
      String resultNewList = mapper.writeValueAsString(listNewInsert);
      String resultAlreadyList = mapper.writeValueAsString(listAlreadyExist);
	  response.setContentType("application/json;charset=utf-8");
       
	  response.getWriter().print(resultNewList);
        

    }
	@RequestMapping(value = "updateDuplicateData")
	public String updateDuplicateData(HttpServletRequest request,RedirectAttributes redirectAttributes){
		Object[] updateData = request.getParameterValues("checkbox");
		System.out.println("============length:"+updateData.length);
		if(updateData.length>1){
			for(int i = 0;i<updateData.length;i++){
				Customer customer = new Customer();
				if(Integer.parseInt((String)updateData[i])!=-1){
					
					customer = listNewInsert.get(Integer.parseInt((String)updateData[i]));
					customer.setProvince(customer.getProvince());
					customer.setCity(customer.getCity());
					customer.setContent(customer.getContent());
					customer.setId(listAlreadyExist.get(Integer.parseInt((String)updateData[i])).getId());
				
					customerService.save(customer);
					//listNewInsert.remove(Integer.parseInt((String)updateData[i]));
					
				}
			}
		}
		return "redirect:"+Global.getAdminPath()+"/crm/dataImport/baseInformation/industry";
	}
	
	
}
